package com.zy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zy.constant.MessageConstant;
import com.zy.dao.MemberDao;
import com.zy.dao.OrderDao;
import com.zy.dao.OrderSettingDao;
import com.zy.entity.Result;
import com.zy.pojo.Member;
import com.zy.pojo.Menu;
import com.zy.pojo.Order;
import com.zy.pojo.OrderSetting;
import com.zy.service.OrderService;
import com.zy.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass=OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

	//预约人数表
	@Autowired
	private OrderSettingDao orderSettingDao;

	//人员信息表
	@Autowired
	private MemberDao memberDao;

	//人员-套餐 的中间表--记录谁预约了什么套餐
	@Autowired
	private OrderDao orderDao;

	public Result order(Map map) throws Exception {
		//1、判断该该日期是否已经设置
		String orderDate = (String) map.get("orderDate");
		Date date = DateUtils.parseString2Date(orderDate);
		OrderSetting orderString=orderSettingDao.findByOrderDate(date);
		if(orderString == null){
			return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
		}
		//2、检查当前预约人数是否已满
		int number = orderString.getNumber();
		int reservations = orderString.getReservations();
		if(reservations>=number){
			return new Result(false,MessageConstant.ORDER_FULL);
		}
		//3、判断是否用户注册,并且判断重复约（一个人同一天不能约同一个人套餐）
		//检查该人是否为会员（是否已经注册）----根据手机号判断(这里用手机号作为区分用户的唯一值)
		String telephone = (String) map.get("telephone");
		Member member =memberDao.findByTelephone(telephone);

		//判断手机号是否已经注册
		if(member != null){
			String name = (String) map.get("name");
			//判断 用户的名称 和 登入的名称是否一致
			if(!member.getName().equals(name)){
				return new Result(false,"该手机号已注册");
			}
			//重复预约判断
			Integer menuId = member.getId();
			//预约日期---date
			Integer setmealid = Integer.parseInt((String) map.get("setmealId"));
			// 会员id  +  预约日期 + 套餐  --->判断是否重复预约
			//查询中间表---是否已经预约
			Order order = new Order(menuId,date,setmealid);
			List<Order> list=orderDao.findByCondition(order);
			if(list != null && list.size() > 0){
				//已经预约
				return new Result(false,MessageConstant.HAS_ORDERED);
			}
		}else {
			//用户自动注册
			member=new Member();
			member.setName((String) map.get("name"));
			member.setPhoneNumber(telephone);
			member.setIdCard((String) map.get("idCard"));
			member.setSex((String) map.get("sex"));
			member.setRegTime(new Date());
			memberDao.add(member);  //要返回新注册人的id
		}
		//4、所有的情况已经排除，往中间表插入数据，进行注册
		Order orderNew=new Order(member.getId(),date, (String)map.get("orderType"), Order.ORDERSTATUS_NO, Integer.parseInt((String)map.get("setmealId")));
		orderDao.add(orderNew);
		//更新预约人数--根据日期修改预约人数(在sql中+1)
		orderSettingDao.editReservationsByOrderDate(orderString);
		return new Result(true,MessageConstant.ORDER_SUCCESS,orderNew.getId());
	}

	@Override
	public Map findById(Integer id) {
		return orderDao.findById(id);
	}

}
